package define;

import main.ErrorException;

public class Common 
{	
	public static boolean m_hasUI = false;
	public static boolean m_isRunning = false;
	public static String m_path = "./";
	
	public static String m_scriptSheet = Config.SCRIPT_SHEET;
	
	public static boolean m_isBreakFor = false;
	
	public static Integer isInt(String number)
	{
		try 
		{
			return Integer.parseInt(number);
		} 
		catch(Exception e) 
		{
			return null;
		}
	}
	
	public static Double isNumber(String number)
	{
		try 
		{
			return Double.parseDouble(number);
		} 
		catch(Exception e) 
		{
			return null;
		}
	}
	
	public static String getExtendName(String oldName) throws ErrorException
	{
		int i = oldName.length() - 1;
		while (i > 0 && oldName.charAt(i) != '.') 
		{			
			--i;
		}
		if(i + 1 >= oldName.length())
		{
			throw new ErrorException(ErrorDefine.E_EXCEL_EXTEND_ERROR, oldName, Config.NO_POSITION);
		}
		return oldName.substring(i + 1);
	}
	
	public static String getPath(String fileName)
	{
		int i = fileName.length() - 1;
		while(i >= 0 && fileName.charAt(i) != '/' && fileName.charAt(i) != '\\')
		{
			--i;		
		}
		return fileName.substring(0, i + 1);
	}
	
	public static String getWithoutExtendName(String fileName) throws ErrorException
	{
		int i = fileName.length() - 1;
		while (i > 0 && fileName.charAt(i) != '.') 
		{			
			--i;
		}
		int end = i;
		if(end == fileName.length() - 1)
		{
			throw new ErrorException(ErrorDefine.E_EXCEL_EXTEND_ERROR, fileName, Config.NO_POSITION);
		}
		while (i >= 0 && (fileName.charAt(i) != '/') && (fileName.charAt(i) != '\\')) 
		{			
			--i;
		}
		if(i == 0)
		{
			--i;
		}
		if(i + 1 >= end)
		{
			throw new ErrorException(ErrorDefine.E_EXCEL_NAME_ERROR, fileName, Config.NO_POSITION);
		}
		return fileName.substring(i + 1, end);
	}
	
	public static String buildString(int[] array, int start, int end) throws ErrorException
	{
		StringBuffer temp = new StringBuffer();
		boolean isBlock = false;
		for(;start <= end; ++start)
		{
			if(array[start] <= 0) 
			{
				if(array[start] == Keyword.E_ESC_KEY.getCode())
				{
					continue;
				}
				else if(array[start] == Keyword.E_WHOLE_WORD_START.getCode())
				{
					isBlock = true;
					continue;
				}
				else if(array[start] == Keyword.E_WHOLE_WORD_END.getCode())
				{
					if(isBlock)
					{
						isBlock = false;
						continue;
					}
					else
					{
						throw new ErrorException(ErrorDefine.E_EXP_NOT_MATCH_ERROR, start);
					}
				}
				else
				{
					throw new ErrorException(ErrorDefine.E_BUILD_STRING_ERROR, start);
				}
			}
			else
			{
				temp.append((char)array[start]);
			}
		}
		return temp.toString();
	}
	
	public static boolean isCharReadable(int[] array, int position)
	{
		return array[position] > 32 || array[position] < Keyword.E_ESC_KEY.getCode();
	}
	
	public static int findRelative(int aimPosition, int[] array, int end)
	{
		Keyword aim = Keyword.isRelative(array[aimPosition]);
		int count = 1;
		assert aim != null: "findRelative() wrong, no relative";
		for(int i = aimPosition + 1; i <= end; ++i)
		{
			if(array[i] == aim.getRelativeCode())
			{
				--count;
				if(count == 0)
				{
					return i;
				}
			}
			if(array[i] == aim.getCode())
			{
				++count;
			}
		}
		return Config.NO_FIND;
	}
	
	public static int find(int aim, int[] array, int start, int end)
	{
		for(; start <= end; ++start)
		{
			if(aim == array[start])
			{
				return start;
			}
		}
		return Config.NO_FIND;
	}
	
	public static boolean isBlank(int code)
	{
		return code >= 0 && code <= 32;
	}
	
	public static String removeBlankAtEnd(String input)
	{
		int index = input.length() - 1;
		while(index >= 0 && isBlank(input.charAt(index))) 
		{
			--index;			
		}
		if(index < input.length() - 1)
		{
			if(index < 0)
			{
				return "";
			}
			else
			{
				return input.substring(0, index + 1);
			}
		}
		return input;
	}
	
	public static String removeBlank(String input)
	{
		int start = 0;
		int end = input.length() - 1;
		for(; start <= end; ++start)
		{
			if(!Common.isBlank(input.charAt(start)))
			{
				break;
			}
		}
		if(start > end)
		{
			return "";
		}
		while(end >= start && Common.isBlank(input.charAt(end))) 
		{
			--end;			
		}
		if(start > end)
		{
			return "";
		}
		else {
			return input.substring(start, end + 1);
		}
	}
}
